package com.kele.user.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kele.common.model.response.Result;
import com.kele.model.user.User;
import com.kele.user.mapper.UserMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    UserMapper userMapper;

    final String prefix ="smsCode_";

    public Result sendSmsCode(String phone){

        Random random = new Random();
        int code = random.nextInt(1000000);
        String smsCode = String.format("%06d",code);

        //保存到redis 中
        redisTemplate.opsForValue().set(prefix + phone,smsCode,60L, TimeUnit.SECONDS);


        //SMSUtils.sendSMS(phone,smsCode);

        Map<String,String> map = new HashMap<>();

        map.put("signName","思远");
        map.put("templateCode","SMS_158845051");

        map.put("mobile",phone);
        Map templateParam_map = new HashMap();
        templateParam_map.put("code",smsCode);
        map.put("templateParam", JSON.toJSONString(templateParam_map));

        rabbitTemplate.convertAndSend("","sms",map);


        System.out.println("短信验证码：" + phone + "--->" +smsCode);
        return new Result(false,"0","发送成功");
    }

    public Result insert(User user, String smsCode ){

        String phone = user.getPhone();
        String redis_code = (String)redisTemplate.opsForValue().get(prefix + phone);
        if(redis_code==null ){
            return new Result(true,"1","验证码过期");
        }
        if(!redis_code.equals(smsCode)){
            return new Result(false,"0","验证码错误");
        }

        User userByPhoneOrEmail = getUserByPhoneOrEmail(phone, user.getEmail());

        if(userByPhoneOrEmail!=null){
            return new Result(false,"0","手机号或者Email已经注册");
        }

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encodePWD = bCryptPasswordEncoder.encode(user.getPassword());
        user.setPassword(encodePWD);
        userMapper.insert(user);
        return new Result(true,"1","注册成功");
    }

    public User getUserByPhoneOrEmail(String phone,String email){

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getPhone,phone)
                .or()
                .eq(User::getEmail,email);

        List<User> users = userMapper.selectList(queryWrapper);
        if(users.size()>0){
            return users.get(0);
        }
        return null;
    }

    public Result getSmsCodeTTL(String phone) {

        //Boolean aBoolean = redisTemplate.hasKey(prefix + phone);
        Long expire = redisTemplate.getExpire(prefix + phone);
        return new Result(true,"1",expire.toString());
    }
}
